package com.ikun.controller;

import cn.hutool.core.util.StrUtil;
import com.ikun.core.pojo.dto.PageDTO;
import com.ikun.core.pojo.dto.TestDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Tag(name = "App", description = "最基础的接口，可做测试")
@RestController
@RequestMapping("/app")
public class AppController {

    @Operation(summary = "获取欢迎信息")
    @GetMapping("/hello")
    public Object hello() {
        Map<String, String> map = new HashMap<>();
        map.put("code", "0");
        map.put("msg", "success");
        map.put("data", "欢迎使用");
//        throw new BusinessException(ErrorEnum.PAYMENT_ERROR);
        return map;
    }

    @Operation(summary = "上传文件")
    @PostMapping("/upload")
    public String uploadFile(@RequestParam("hh") MultipartFile ikun) {
        if (!ikun.isEmpty()) {
            // 处理文件上传
            System.out.println("文件名: " + ikun.getOriginalFilename());
            // 保存文件等操作...
        }
        return "上传成功";
    }

    @Operation(summary = "查询参数0", description = "不使用包装类，直接使用基本类型")
    @GetMapping("/query0")
    public Object query0(String name, int age) {
        log.info("======= query0 =========");
        log.info("name={}, age={}", name, age);
        log.info("======= query0 =========");
        return StrUtil.format("name={}, age={}", name, age);
    }

    @Operation(summary = "查询参数0", description = "Post请求，不使用包装类，直接使用基本类型")
    @PostMapping("/query0")
    public Object query0Post(String name, int age) {
        log.info("======= query0 post =========");
        log.info("name={}, age={}", name, age);
        log.info("======= query0 =========");
        return StrUtil.format("name={}, age={}", name, age);
    }

    @Operation(summary = "查询参数1", description = "使用包装类，可以避免空指针异常")
    @GetMapping("/query1")
    public Object query1(String name, Integer age, Boolean flag) {
        log.info("======= query1 =========");
        log.info("name={}, age={}, flag={}", name, age, flag);
        log.info("======= query1 =========");
        return StrUtil.format("name={}, age={}, flag={}", name, age, flag);
    }

    @Operation(summary = "查询参数1", description = "使用包装类，可以避免空指针异常")
    @PostMapping("/query1")
    public Object query1post(String name, Integer age, Boolean flag) {
        log.info("======= query1 =========");
        log.info("name={}, age={}, flag={}", name, age, flag);
        log.info("======= query1 =========");
        return StrUtil.format("name={}, age={}, flag={}", name, age, flag);
    }

    @Operation(summary = "查询参数2")
    @GetMapping("/query2")
    public Object query2(@RequestParam() String name, @RequestParam(required = false, defaultValue = "18") Integer age) {
        log.info("======= query2 =========");
        log.info("name={}, age={}", name, age);
        log.info("======= query2 =========");
        return StrUtil.format("name={}, age={}", name, age);
    }

    @Operation(summary = "查询参数3")
    @GetMapping("/query3")
    public Object query3(PageDTO pageDTO) {
        StrUtil.format("pageDTO={}", pageDTO);
        return pageDTO;
    }

    @Operation(summary = "查询参数4", description = "自定义DTO参数")
    @GetMapping("/query4")
    public Object query4(TestDTO testDTO) {
        log.info("======= query4 =========");
        log.info("testDTO={}", testDTO);
        log.info("======= query4 =========");
        return testDTO;
    }

    @Operation(summary = "请求体参数", description = "自定义DTO参数")
    @PostMapping("/body")
    public Object body(@RequestBody TestDTO testDTO) {
        log.info("======= body =========");
        log.info("testDTO={}", testDTO);
        log.info("======= body =========");
        return testDTO;
    }

    @Operation(summary = "路径参数0")
    @GetMapping("/path0/{id}")
    public Object path0(@PathVariable int id) {
        log.info("======= path0 =========");
        log.info("id={}", id);
        log.info("======= path0 =========");
        return StrUtil.format("id={}", id);
    }

    @Operation(summary = "路径参数1", description = "使用包装类，可以避免空指针异常")
    @GetMapping("/path1/{id}")
    public Object path1(@PathVariable Integer id) {
        log.info("======= path1 =========");
        log.info("id={}", id);
        log.info("======= path1 =========");
        return StrUtil.format("id={}", id);
    }

    @Operation(summary = "路径参数1", description = "使用包装类，可以避免空指针异常")
    @GetMapping("/path1/{id}/aaa")
    public Object path11(@PathVariable Integer id) {
        log.info("======= path1 =========");
        log.info("id={}", id);
        log.info("======= path1 =========");
        return StrUtil.format("id={}", id);
    }

    @Operation(summary = "路径参数2", description = "String 接数字参数，在末尾")
    @GetMapping("/path2/{id}")
    public Object path2(@PathVariable String id) {
        log.info("======= path2 =========");
        log.info("id={}", id);
        log.info("======= path2 =========");
        return StrUtil.format("id={}", id);
    }

    @Operation(summary = "路径参数3", description = "String 接数字参数，在中间")
    @GetMapping("/path3/aaa")
    public Object path31(String id) {
        log.info("======= path31 =========");
        log.info("id={}", id);
        log.info("======= path31 =========");
        return StrUtil.format("id={}", id);
    }

    @Operation(summary = "路径参数3", description = "String 接数字参数，在中间")
    @GetMapping("/path3/{id}/aaa")
    public Object path3(@PathVariable() String id) {
        log.info("======= path3 =========");
        log.info("id={}", id);
        log.info("======= path3 =========");
        return StrUtil.format("id={}", id);
    }

    @Operation(summary = "路径参数4", description = "boolean 类型")
    @GetMapping("/path4/{id}")
    public Object path4(@PathVariable() boolean id) {
        log.info("======= path4 =========");
        log.info("id={}", id);
        log.info("======= path4 =========");
        return StrUtil.format("id={}", id);
    }

    @Operation(summary = "路径参数5", description = "Boolean 类型")
    @GetMapping("/path5/{id}")
    public Object path5(@PathVariable() Boolean id) {
        log.info("======= path5 =========");
        log.info("id={}", id);
        log.info("======= path5 =========");
        return StrUtil.format("id={}", id);
    }
}
